import com.alibaba.fastjson.JSONObject;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Objects;
import java.util.stream.Collectors;

public class Demo {

    private static final String code= "code"; // 更换成你的授权码
    public static void main(String[] args) throws Exception {
        JSONObject params = new JSONObject();
        String signature = getSignature(params, code);

        System.out.println(signature );
    }

    private static String getSignature(JSONObject params, String key) throws Exception {
        String data = params.keySet()
                .stream()
                .sorted()
                .map(params::get)
                .filter(Objects::nonNull)
                .map(String::valueOf)
                .collect(Collectors.joining(""));
        byte[] keyBytes = key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte b : rawHmac) {
            sb.append(byteToHexString(b));
        }
        return sb.toString();
    }

    private static String byteToHexString(byte ib) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0f];
        ob[1] = Digit[ib & 0X0F];
        return new String( ob );
    }
}